package fivestar.model.dao.hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import fivestar.model.dao.CountryDao;
import fivestar.model.entity.Country;
import fivestar.util.HibernateUtil;

public class CountryDaoHibernate implements CountryDao{

    Session session = null;
    Transaction tx = null;
    
    @Override
    public void create(Country country) throws RuntimeException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void delete(Country country) throws RuntimeException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void update(Country country) throws RuntimeException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Country find(int id) throws RuntimeException {
		Country country = null;
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from Country where countryId= :countryId");
			query.setInteger("countryId", id);
			country = (Country) query.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return country;
    }

    @SuppressWarnings("unchecked")
	@Override
    public List<Country> findAll() throws RuntimeException {
       List<Country> lista = null; 
         session = HibernateUtil.getSessionFactory().openSession();
        try {
            tx = session.beginTransaction();
            lista = session.createQuery("from Country").list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return lista;
    }

    @Override
    public Short findID(String country) throws RuntimeException {
        Short id = null; 
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            tx = session.beginTransaction(); 
            Query query = session.createQuery("select c.countryId from Country c where c.country=:country");
            query.setString("country", country);
            id = (Short) query.uniqueResult();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return id;
    }

	@Override
	public Short findByCity(int id) throws RuntimeException {
		Short country = null;
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			tx = session.beginTransaction();
			//Query query = session.createQuery("select co.country from City  as ci inner join ci.country co where ci.cityId = :cityId");   
			Query query = session.createQuery("select co.countryId from City  as ci inner join ci.country co where ci.cityId = :cityId");
			query.setInteger("cityId", id); 
			country = (Short) query.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
 	return country;
    }

}
